package cn.hy.redis.limiter.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Objects;

/**
 * 异常工具类
 *
 */
public class ExceptionUtil {

    private ExceptionUtil() {
    }


    public static void rethrowException(String message, Throwable cause) {
        throw new RuntimeException(message, cause);
    }

    public static void rethrowException(String message) {
        rethrowException(message, null);
    }

    public static String extractStackTrace(Throwable error) {
        if (Objects.isNull(error)) {
            return "";
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            error.printStackTrace(pw);
            sw.flush();
            return sw.toString();
        } finally {
            pw.close();
        }
    }

}
